package br.edu.ufcg.splab.io;

import static org.junit.Assert.assertTrue;

import java.io.File;

import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaderJDOMFactory;
import org.jdom2.input.sax.XMLReaderXSDFactory;
import org.junit.Test;

import br.edu.ufcg.splab.core.InterfaceGenerate;
import br.edu.ufcg.splab.core.InterfaceGraph;
import br.edu.ufcg.splab.core.graph.Graph;
import br.edu.ufcg.splab.generator.TestCaseGenerator;
import br.edu.ufcg.splab.generator.TestSuite;
import br.edu.ufcg.splab.parser.GraphToAnnotatedGraphParser;
import br.edu.ufcg.splab.util.ReadTGF;

public class TestLinkXMLWriterTest {

	private final static String SCHEMA_NAME = "TestlinkSchema.xsd";
	
	@Test
	public void testWithOneCondition(){
		try {
			String graphOneConditionBeginningFilePath = "./test-data/input/OneConditionBegining.tgf";
			ReadTGF TGFReader = new ReadTGF();
			InterfaceGraph correspondentGraph = TGFReader.getGraph(graphOneConditionBeginningFilePath);
			InterfaceGraph annotatedGraph = new GraphToAnnotatedGraphParser().graphContraction(correspondentGraph);
			
					assertTrue(annotatedGraph.getEdges().size() == 5);
					assertTrue(annotatedGraph.getStates().values().size() == 6);
			
			InterfaceGenerate generator = new TestCaseGenerator();
			TestSuite generatedSuite = generator.generate(annotatedGraph);
			
					assertTrue(generatedSuite.size() == 2);
			
			TestLinkXMLWriter writer = new TestLinkXMLWriter();
			writer.write(generatedSuite, "./test-data/output/OneConditionBegining.xml");
			
			File xsdfile = new File(SCHEMA_NAME);
			XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(xsdfile);
			SAXBuilder builder = new SAXBuilder(schemafac);
			File xmlfile = new File("./test-data/output/OneConditionBegining.xml");
			Document validdoc = builder.build(xmlfile);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testWithTwoConditionInBegining(){
		try {
			String graphTwoConditionInBeginingFilePath = "./test-data/input/TwoConditionInBegining.tgf";
			ReadTGF TGFReader = new ReadTGF();
			InterfaceGraph correspondentGraph = TGFReader.getGraph(graphTwoConditionInBeginingFilePath);
			InterfaceGraph annotatedGraph = new GraphToAnnotatedGraphParser().graphContraction(correspondentGraph);
			
					assertTrue(annotatedGraph.getEdges().size() == 6);
					assertTrue(annotatedGraph.getStates().values().size() == 7);
			
			InterfaceGenerate generator = new TestCaseGenerator();
			TestSuite generatedSuite = generator.generate(annotatedGraph);
			
					assertTrue(generatedSuite.size() == 2);
			
			TestLinkXMLWriter writer = new TestLinkXMLWriter();
			writer.write(generatedSuite, "./test-data/output/TwoConditionInBegining.xml");
			
			File xsdfile = new File(SCHEMA_NAME);
			XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(xsdfile);
			SAXBuilder builder = new SAXBuilder(schemafac);
			File xmlfile = new File("./test-data/output/OneConditionBegining.xml");
			Document validdoc = builder.build(xmlfile);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testWithTwoConditionAlong(){
		try {
			String graphTwoConditionAlongFilePath = "./test-data/input/TwoConditionAlong.tgf";
			ReadTGF TGFReader = new ReadTGF();
			InterfaceGraph correspondentGraph = TGFReader.getGraph(graphTwoConditionAlongFilePath);
			InterfaceGraph annotatedGraph = new GraphToAnnotatedGraphParser().graphContraction(correspondentGraph);
			
					assertTrue(annotatedGraph.getEdges().size() == 11);
					assertTrue(annotatedGraph.getStates().values().size() == 12);
			
			InterfaceGenerate generator = new TestCaseGenerator();
			TestSuite generatedSuite = generator.generate(annotatedGraph);
			
					assertTrue(generatedSuite.size() == 3);
			
			TestLinkXMLWriter writer = new TestLinkXMLWriter();
			writer.write(generatedSuite, "./test-data/output/TwoConditionAlong.xml");
			
			File xsdfile = new File(SCHEMA_NAME);
			XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(xsdfile);
			SAXBuilder builder = new SAXBuilder(schemafac);
			File xmlfile = new File("./test-data/output/TwoConditionAlong.xml");
			Document validdoc = builder.build(xmlfile);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testWithDefaultSteps(){
		try {
			String graphWithDefaultStepsFilePath = "./test-data/input/WithDefaultSteps.tgf";
			ReadTGF TGFReader = new ReadTGF();
			Graph correspondentGraph = TGFReader.getGraph(graphWithDefaultStepsFilePath);
			
					assertTrue(correspondentGraph.getEdges().size() == 6);
					assertTrue(correspondentGraph.getStates().values().size() == 7);
			
			InterfaceGenerate generator = new TestCaseGenerator();
			TestSuite generatedSuite = generator.generate(correspondentGraph);
			
					assertTrue(generatedSuite.size() == 2);
			
			TestLinkXMLWriter writer = new TestLinkXMLWriter();
			writer.write(generatedSuite, "./test-data/output/WithDefaultSteps.xml");
			
			File xsdfile = new File(SCHEMA_NAME);
			XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(xsdfile);
			SAXBuilder builder = new SAXBuilder(schemafac);
			File xmlfile = new File("./test-data/output/WithDefaultSteps.xml");
			Document validdoc = builder.build(xmlfile);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
